Updating and deleting records in a MySQL database files, involves routine
CRUD operations. There are also common
operations for a Python based application that interacts with a MySQL database. Over the next few minutes, you'll learn how to
execute, update and delete operations in a back-end MySQL
database in Python. Little Lemon needs to update
and delete the records of a restaurant guests in the bookings table in their
database using Python. You need to help Little
Lemon with this task. But first, let's take a quick
look at the bookings table. This table holds the
book data of each guest. This includes their
booking ID, table number, first and last
name, booking slot and the ID of the waiter. Little Lemon must
update and delete data within this
table using Python. Let's begin with
updating records. As you know from
previous courses, data is updated on a database
using update statements. Little Lemon needs to update
the booking data for guest. Let's see if you
can help them out. You can start by writing
SQL update query that uses a where clause to
let Little Lemon update a guest's table number. This update query is
created as a string. Bookings, SET table No 10, where booking ID equals six. It then pass through a string
called update bookings. Little Lemon just need to update the specific values
of each query. Let's test this
string by updating the bookings information
for guest 6, Diana Pinto, who has been
moved to table number 10. Use the execute method
to run the query and update the records in the
bookings table in the database. Cursor.execute update bookings. To commit the changes
to the database, use the commit method. To check if the data
was updated correctly, you can run the print cells
of the Jupiter Notebook. The guest with the book ID of six is now assigned
the table of 10. The upgrade query has worked. There are also times when
guests cancel their bookings. When this happens,
Little Lemon needs to delete the records
from the database. To carry out this task, write an SQL delete
statement that deletes the record of a specific guest from the bookings table. Your statement can use a
where clause of booking ID. Delete from bookings, where booking ID equals four. You can test this
query by deleting the booking information
from Marcos Romero, who was assigned the
booking ID of four. Use the execute method
to run the query, then commit the changes
to the database. Connection.commit. Now it's time to check if the
the query worked. Run the print cells of the
Jupiter Notebook again. There's no data in the
table for Marcos Romero. This means that your delete
statement was successful. You can also amend the where clause in your
delete statement to check for null values next
to table employee ID's. If the value is known, then you can set the
bookings to be deleted. You now know how to perform
update and delete operations in MySQL database using
Python. Great work.